package Proxy

import scala.util.Random

//处于销售状态,正在出糖果
class SoldingState extends State {
  @transient
  private var mCandyMachine: CandyMachine = _

  def this(mcm: CandyMachine) {
    this
    this.mCandyMachine = mcm
  }

  override def insertCoin(): Unit = {
    println("please wait! we are giving you a candy !")
  }

  override def returnCoin(): Unit = {
    println("coin return !")
  }

  override def turnCrank(): Unit = {
    println("crank turn ...!")
    val ranwinner = new Random()
    val winner = ranwinner.nextInt(10)
    if (winner == 0) {

    } else {

    }
  }

  override def printState(): Unit = {
    println("****SoldOutState******")
  }

  //没有逻辑
  override def dispense(): Unit = {
    mCandyMachine.releaseCandy() //数量-1
    if (mCandyMachine.getCount() > 0) {
      //如果还有糖,则进入readyState
      mCandyMachine.setState(mCandyMachine.mOnReadyState)
    } else {
      println("Oo, out of candies")
      mCandyMachine.setState(mCandyMachine.mSoldOutState)
    }
  }

  override def getStateName(): String = {
    "SoldingState"
  }
}
